import {
  myRequesy
} from '../../request/api.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    modalName: '',
    desc_imgs: [],
    disabled: false,

    oreason: '',

    list: [
      '功能异常',
      '产品建议',
      '虚假活动',
      '色情暴力',
      '恶意营销',
      '伪冒侵权',
      '诱导关注',
      '欺诈行为',
    ],
    param: {
      miniprogram_name: '青年摄影',
      nickName: '',
      feedback_type: '功能异常',
      avatarUrl: '',
      uid: '',
      content: '',
      is_feedback: true,
      openid: '',
      photos: [],
      wechat: '',
      // content: place.concat(that.data.feedbackContent),
      // wechat: that.data.wechat,
      // photos: that.data.files.map(e => e.url),
      // is_feedback: true,
      // miniprogram_id: that.data.miniprogram_id
    },
    feedbackIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.hideShareMenu({
    //   menus: ['shareAppMessage', 'shareTimeline']
    // })
    this.setData({
      [`param.miniprogram_id`]: parseInt(options.vid) || 0,
      title: options.title,
      uplaodFile: this.uplaodFile.bind(this)
    })
    this.getUser()
    this.getUserOpenid()
  },

  //获取用户信息
  getUser() {
    let that = this
    myRequesy({
      url: 'user/wechat-info'
    }).then(res => {
      console.log(res);
      if (res.data.code === 0) {
        that.setData({
          [`param.nickName`]: res.data.data.nickName,
          [`param.avatarUrl`]: res.data.data.avatarUrl,
        })
      }
    })
  },

  //获取用户id和openid
  getUserOpenid() {
    let that = this
    myRequesy({
      url: 'user/openid'
    }).then(res => {
      console.log(res);
      if (res.data.code === 0) {
        that.setData({
          [`param.uid`]: res.data.data.id,
          [`param.openid`]: res.data.data.openid,
        })
      }
    })
  },


  // 跳转到反馈记录页
  enterReportList() {
    wx.navigateTo({
      url: '/pages/reportList/reportList',
    })
  },

  // 预览图片
  openCode() {
    wx.previewImage({
      urls: ['https://tp.needyoutd.com/vote/data/image/tp_kefu.jpg']
    })
  },

  // async getUser() {
  //   wx.showLoading()
  //   const res = await api.postQueryVote()
  //   const result = await api.getOpenid()
  //   this.setData({
  //     [`param.openid`]: result.data.openid,
  //     [`param.nickName`]: res.data.nickName,
  //     [`param.avatarUrl`]: res.data.avatarUrl,
  //     [`param.uid`]: res.data.uid,
  //   })
  //   wx.hideLoading({
  //     success: (res) => {},
  //   })
  // },


  //根据点击获取不同的反馈类型
  changeType(e) {
    this.setData({
      feedbackIndex: parseInt(e.currentTarget.dataset.index),
      [`param.feedback_type`]: e.currentTarget.dataset.item
    })
  },

  changeFeedbackType(e) {
    console.log(e, 'aaae')

  },

  //判断当前选中的是反馈或投诉
  changeFdtype(e) {
    console.log(e);
    // e.currentTarget.dataset
    const v = Boolean(parseInt(e.currentTarget.dataset.value))
    const index = v ? 0 : 2
    this.setData({
      feedbackIndex: index,
      // isFeedBack:v,
      [`param.is_feedback`]: v,
      [`param.feedback_type`]: this.data.list[index]
    })
  },

  //获取用户微信号
  bindwechat(e) {
    this.setData({
      [`param.wechat`]: e.detail.value
    })
  },

  bindContent(e) {
    this.setData({
      [`param.content`]: e.detail.value
    })
  },

  commit() {
    let that = this
    if (that.data.param.content + '' === '') {
      wx.showToast({
        title: '请填写反馈内容',
        icon: 'none'
      })
      return
    }
    if (that.data.param.wechat + '' === '') {
      wx.showToast({
        title: '请填写微信号',
        icon: 'none'
      })
      return
    }

    if (that.data.feedbackIndex === 2) {
      wx.requestSubscribeMessage({
        tmplIds: ['pv2QC3t52BsEO6nnDusPMmj9vXPpS0b9vUcCfYQ4Omg', 'Ke1p0Jn84Q_5XVYM6TT5aWb1Z4-oy7bJ2odnnH88kOE', '8pUorSTDgk0nqAU7v6GxRM3LP2RY_EmDgcrimSx1X0Q'],
        success() {
          wx.request({
            url: 'https://enroll.needyoutd.com/admin/send_feedback',
            method: 'POST',
            data: that.data.param,
            success: result => {
              wx.hideLoading({
                success: (res) => {},
              })
              if (result.data.err_code === 0) {
                wx.showModal({
                  title: '提示',
                  content: '提交成功',
                  showCancel: false,
                  success(res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.navigateBack()
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              }
            }
          })
        }
      })
    } else {
      wx.request({
        url: 'https://enroll.needyoutd.com/admin/send_feedback',
        method: 'POST',
        data: that.data.param,
        success: result => {
          wx.hideLoading({
            success: (res) => {},
          })
          if (result.data.err_code === 0) {
            wx.showModal({
              title: '提示',
              content: '提交成功',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.navigateBack()
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      })
    }


    // wx.requestSubscribeMessage({
    //   // tmplIds: ['41q6NT3G2k-zQ0kF2S2VqiM7MWIWLWoas1ZYifyAU-4'],
    //   success(res) {
    //     wx.showLoading({
    //       title: '提交中',
    //     })

    //   }
    // })

  },

  bindInput(e) {
    this.setData({
      oreason: e.detail.value
    })
  },

  changeFeedbackType(e) {
    this.setData({
      [`param.feedback_type`]: e.detail.value
    })
  },

  radioChange(e) {
    const v = Boolean(parseInt(e.detail.value))
    // let v = parseInt(e.detail.value)
    const index = v ? 0 : 2
    this.setData({
      feedbackIndex: index,
      // isFeedBack:v,
      [`param.is_feedback`]: v,
      [`param.feedback_type`]: this.data.list[index]

    })
  },

  closeModal() {
    this.setData({
      show: false,
    })
  },

  showModal(e) {
    const {
      index
    } = e.currentTarget.dataset
    this.setData({
      resIndex: index,
      show: true,
    })
  },

  uplaodFile(e) {
    console.log(e);

    let that = this
    // 文件上传的函数，返回一个promise
    return new Promise((resolve, reject) => {
      const tempFilePaths = e.tempFilePaths
      wx.showToast({
        title: '上传中...',
        icon: 'loading'
      })
      for (let i = 0; i < tempFilePaths.length; i++) {
        let that = this
        wx.uploadFile({
          url: 'https://lottery.qnsyw.cn/api/upload-img',
          filePath: tempFilePaths[i],
          name: 'imgFile',
          formData: {
            'imgIndex': i
          },
          header: {
            'content-type': 'multipart/form-data',
            'Authorization': wx.getStorageSync('userInfo').token
          },
          success: function (res) {
            var data = JSON.parse(res.data);
            console.log(data);
            if (i === tempFilePaths.length - 1) {
              wx.hideToast()
              that.setData({
                [`param.photos`]: [...that.data.param.photos, {
                  url: data.data.imgUrl
                }]
              })
            }
          },
          fail: function (err) {
            wx.hideToast();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
            })
          }
        });
      }
      // for (let i = 0; i < tempFilePaths.length; i++) {
      //   wx.uploadFile({
      //     filePath:tempFilePaths[i],
      //     name: 'image',
      //     url: 'https://enroll.needyoutd.com/admin/upload_img',
      //     success(res){
      //       console.log(res);

      //     }

      //   })
      // }
    })
  },

  uploadSuccess(e) {
    console.log('upload success', e.detail)
  },

  uploadError(e) {
    console.log('upload error', e.detail)
  },



})